<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();

require_once ("includes/connect.php");
require_once ("includes/function.php");

$db = new myDBS();

$task = $_REQUEST["task"];

$id = $_REQUEST["id"];
$budget_year = $_REQUEST["budget_year"];
$panngan2_id = $_REQUEST["panngan2_id"];
$panngan3_id = $_REQUEST["panngan3_id"];
$money_out2_id = $_REQUEST["money_out2_id"];
$money_out3_id = $_REQUEST["money_out3_id"];

$month_year = $_REQUEST["month_year"];
$amount = $_REQUEST["amount"];

$vender_id = $_REQUEST["vender_id"];
$comments = $_REQUEST["comments"];
$date_create = $_REQUEST["date_create"];

$cancel_date = $_REQUEST["cancel_date"];
$cancel_reason = $_REQUEST["cancel_reason"];

$_SESSION["lvl1_id"] = $_REQUEST["lvl1_id"];
$lvl1_id = $_REQUEST["lvl1_id"];

//echo "xxxx".$_SESSION["lvl1_id"];

switch ($task) {
    case "new":
        $pet_id = $db->get_running($budget_year);

//        echo $pet_id;
        
        $budget_out = get_budget_out($money_out2_id, $money_out3_id);
        $budget_out_use = get_budget_out_use($money_out2_id, $money_out3_id, $budget_year);
        
        $budget_out_remain = $budget_out - $budget_out_use;
        $budget_out_remain_this = $budget_out_remain - $amount;
        
        $sql = "INSERT INTO tbl_petout (petout_id, lvl1_id, panngan2_id, panngan3_id, money_out2_id, money_out3_id"
                . " , month_year, budget_year, vender_id, amount, comments, date_create "
                . " , budget_out_remain, budget_out_remain_this )"
                . " VALUES ('$pet_id', '$lvl1_id', '$panngan2_id', '$panngan3_id', '$money_out2_id', '$money_out3_id' "
                . " , '$month_year', '$budget_year', '$vender_id' "
                . " , '$amount', '$comments', '$date_create' "
                . " , '$budget_out_remain', '$budget_out_remain_this' "
                . ")";

//        echo $sql;
        $db->runSQL($sql) or die(mysql_error());

        break;

    case "edit":
        $sql = "UPDATE tbl_petout "
                . "SET panngan2_id = '$panngan2_id' "
                . ", panngan3_id = '$panngan3_id' "
                . ", money_out2_id = '$money_out2_id' "
                . ", money_out3_id = '$money_out3_id' "
                . ", month_year = '$month_year' "
                . ", budget_year = '$budget_year' "
                . ", amount = '$amount' "
                . ", vender_id = '$vender_id' "
                . ", comments = '$comments' "
                . ", date_create = '$date_create' "
                . " WHERE id = '$id' ";

        //echo $sql;
        $db->runSQL($sql) or die(mysql_error());

        break;

    case "cancel":
        $sql = "UPDATE tbl_petout "
                . " SET cancel_date = '$cancel_date', "
                . " cancel_reason = '$cancel_reason', "
                . " status = 'ยกเลิก' "
                . " WHERE id = '$id'";
        //echo $sql;

        $db->runSQL($sql) or die(mysql_error());
        break;

    case "confirm":

        $budget_year = (date("Y") < 2556) ? date("Y") + 543 : date("Y");

        $finance_id = get_running_finance($budget_year);

        $sql = "UPDATE tbl_petout "
                . "SET confirm = 'ยืนยัน'"
                . ", finance_id = '$finance_id' "
                . " WHERE  id = '$id'";

        $db->runSQL($sql) or die(mysql_error());

        break;
    default:
        break;
}
?>

